package com.ns.dcjh.ui.popup;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.lxj.xpopup.core.BottomPopupView;
import com.ns.dcjh.R;
import com.ns.dcjh.bean.FloatWindow;
import com.ns.dcjh.bean.FullScreen;
import com.ns.dcjh.enums.TextColorEnum;
import com.ns.dcjh.utils.DisplayUtil;
import com.ns.dcjh.utils.FileUtils;

import java.text.DecimalFormat;

public class FloatWindowPopup extends BottomPopupView {

    public static final int TEXT_COLOR_WHITE = 1011;
    public static final int TEXT_COLOR_GRAY = 1012;
    public static final int TEXT_COLOR_ORANGE = 1013;
    public static final int TEXT_COLOR_RED = 1014;
    public static final int TEXT_COLOR_BLUE = 1015;
    public static final int TEXT_COLOR_GREEN = 1016;
    public static final int TEXT_COLOR_BLACK = 1017;

    private RadioGroup textColorRadioGroup;

    private SeekBar floatWindowTextSizeSb;
    private TextView floatWindowSizeText;

    private SeekBar floatWindowCutDownSb;
    private TextView floatWindowCutDownText;

    private SeekBar floatWindowSpeedSb;
    private TextView floatWindowSpeedText;

    private Switch floatWindowReverseSwitch;

    private LinearLayout floatWindowRecoverBtn;

    private TextView floatWindowBtnOpen;

    private OnFullScreenPopupChangeListener onFullScreenPopupChangeListener;

    private FloatWindow floatWindow;

    public FloatWindowPopup(@NonNull Context context, FloatWindow floatWindow) {
        super(context);
        this.floatWindow = floatWindow;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.layout_float_window_setting;
    }


    @Override
    protected void onCreate() {
        super.onCreate();
        initView();

        /**
         * 字体大小设置
         */
        floatWindowTextSizeSb.setProgress((int) floatWindow.textSize-12); //设置默认20sp
        floatWindowSizeText.setText((int) floatWindow.textSize +"");
        floatWindowTextSizeSb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                int size = 12+progress;
                floatWindowSizeText.setText(String.valueOf(size));
                onFullScreenPopupChangeListener.onChangeTextSize(size);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });


        /**
         * 倒计时设置
         */
        floatWindowCutDownSb.setProgress(floatWindow.cutDownTime); //设置默认倒计时5s
        floatWindowCutDownText.setText(floatWindow.cutDownTime+"");
        floatWindowCutDownSb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                floatWindowCutDownText.setText(String.valueOf(progress));
                onFullScreenPopupChangeListener.onChangeCutDownTime(progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        /**
         * 速度设置
         */
        floatWindowSpeedSb.setProgress((int)(floatWindow.speed*10)-1);
        floatWindowSpeedText.setText(floatWindow.speed+"");
        floatWindowSpeedSb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                int speedValue = progress+1;
                float speed = ((float) speedValue) /10;
                DecimalFormat fnum= new DecimalFormat("##0.0");
                String speedStr=fnum.format(speed);
                floatWindowSpeedText.setText(speedStr);
                onFullScreenPopupChangeListener.onChangeSpeed(speed);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });


        /**
         * 字体颜色设置
         */
        textColorRadioGroup.removeAllViews();
        for (TextColorEnum e: TextColorEnum.values()){
            RadioButton radioButton = (RadioButton) View.inflate(getContext(), R.layout.layout_raido_button, null);
            radioButton.setId(e.getColorType());
            radioButton.setText(e.getColorName());

            RadioGroup.LayoutParams lp = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            //设置 meary 宽度
            lp.setMargins(DisplayUtil.dip2px(getContext(), 6),
                    DisplayUtil.dip2px(getContext(), 2),
                    DisplayUtil.dip2px(getContext(), 6),
                    DisplayUtil.dip2px(getContext(), 2));

            if (e.getColorType() == floatWindow.textColorType){
                radioButton.setChecked(true); //设置默认白色
            }

            textColorRadioGroup.addView(radioButton,lp);
        }
        textColorRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                onFullScreenPopupChangeListener.onChangeTextColor(radioGroup.getCheckedRadioButtonId());
            }
        });



        /**
         * 设置镜像
         */
        if (floatWindow.isTextMirror==1){
            floatWindowReverseSwitch.setChecked(true);
        }else {
            floatWindowReverseSwitch.setChecked(false);
        }
        floatWindowReverseSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                onFullScreenPopupChangeListener.onChangeSwitchReverseText(b);
            }
        });

        floatWindowRecoverBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                floatWindow = new Gson().fromJson(FileUtils.getAssetsJson("float_window.json",getContext()), FloatWindow.class);

                floatWindowTextSizeSb.setProgress((int) floatWindow.textSize-12); //设置默认20sp
                floatWindowSizeText.setText((int) floatWindow.textSize +"");

                floatWindowCutDownSb.setProgress(floatWindow.cutDownTime); //设置默认倒计时5s
                floatWindowCutDownText.setText(floatWindow.cutDownTime+"");

                floatWindowSpeedSb.setProgress((int)(floatWindow.speed*10)-1);
                floatWindowSpeedText.setText(floatWindow.speed+"");

                if (floatWindow.isTextMirror==1){
                    floatWindowReverseSwitch.setChecked(true);
                }else {
                    floatWindowReverseSwitch.setChecked(false);
                }

                textColorRadioGroup.removeAllViews();
                for (TextColorEnum e: TextColorEnum.values()){
                    RadioButton radioButton = (RadioButton) View.inflate(getContext(), R.layout.layout_raido_button, null);
                    radioButton.setId(e.getColorType());
                    radioButton.setText(e.getColorName());

                    RadioGroup.LayoutParams lp = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    //设置 meary 宽度
                    lp.setMargins(DisplayUtil.dip2px(getContext(), 6),
                            DisplayUtil.dip2px(getContext(), 2),
                            DisplayUtil.dip2px(getContext(), 6),
                            DisplayUtil.dip2px(getContext(), 2));


                    if (e.getColorType() == floatWindow.textColorType){
                        radioButton.setChecked(true);
                    }

                    textColorRadioGroup.addView(radioButton,lp);
                }

                onFullScreenPopupChangeListener.onRecover();
            }
        });

        floatWindowBtnOpen.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                onFullScreenPopupChangeListener.onCompleteClick();
            }
        });
    }

    private void initView() {
        floatWindowTextSizeSb = findViewById(R.id.floatWindowTextSizeSb);
        floatWindowSizeText = findViewById(R.id.floatWindowSizeText);

        floatWindowCutDownSb = findViewById(R.id.floatWindowCutDownSb);
        floatWindowCutDownText = findViewById(R.id.floatWindowCutDownText);

        floatWindowSpeedSb = findViewById(R.id.floatWindowSpeedSb);
        floatWindowSpeedText = findViewById(R.id.floatWindowSpeedText);


        textColorRadioGroup = findViewById(R.id.floatWindowSettingTextColorRg);

        floatWindowReverseSwitch = findViewById(R.id.floatWindowReverseSwitch);

        floatWindowBtnOpen = findViewById(R.id.floatWindowBtnOpen);

        floatWindowRecoverBtn = findViewById(R.id.floatWindowRecoverBtn);
    }



    public void setOnFullScreenPopupChangeListener(OnFullScreenPopupChangeListener onFullScreenPopupChangeListener){
        this.onFullScreenPopupChangeListener = onFullScreenPopupChangeListener;
    }

}
